import { Vector2D } from '../utils/Vector2D';

// 特效类型定义
export type EffectType = 'pulse' | 'trail' | 'energyField' | 'spaceDistortion';

// 特效参数接口
interface EffectParams {
    intensity: number;
    duration: number;
    color: string;
}

// 食物吞噬效果类型
export type ConsumptionEffectType = 'normal' | 'poison' | 'magnet';

export class SnakeEffectManager {
    // 特效状态
    private effects: Map<EffectType, EffectParams>;
    
    // 蛇身体段直径缩放因子
    private segmentScaleFactors: number[] = [];
    
    // 特效激活阈值
    private readonly PULSE_THRESHOLD = 20;
    private readonly TRAIL_THRESHOLD = 50;
    private readonly ENERGY_FIELD_THRESHOLD = 75;
    private readonly SPACE_DISTORTION_THRESHOLD = 100;
    
    // 膨胀参数
    private readonly BASE_SCALE = 1.0;
    private readonly SCALE_INCREMENT = 0.005; // 每节增加0.5%
    private readonly MAX_SCALE = 1.5; // 最大150%
    
    // 粒子系统
    private particles: Array<{
        position: Vector2D;
        velocity: Vector2D;
        color: string;
        size: number;
        life: number;
        maxLife: number;
    }> = [];
    
    constructor() {
        this.effects = new Map();
    }
    
    /**
     * 更新所有特效
     */
    public updateEffects(segments: Vector2D[], deltaTime: number): void {
        // 更新段大小缩放因子
        this.updateSegmentScales(segments.length);
        
        // 更新特效状态
        this.updateEffectStates(segments.length, deltaTime);
        
        // 更新粒子
        this.updateParticles(deltaTime);
    }
    
    /**
     * 更新段大小缩放因子
     */
    private updateSegmentScales(segmentCount: number): void {
        // 确保数组长度与段数匹配
        while (this.segmentScaleFactors.length < segmentCount) {
            // 计算新段的缩放因子
            const newScale = Math.min(
                this.MAX_SCALE,
                this.BASE_SCALE + (this.segmentScaleFactors.length * this.SCALE_INCREMENT)
            );
            this.segmentScaleFactors.push(newScale);
        }
        
        // 如果段数减少，裁剪数组
        if (this.segmentScaleFactors.length > segmentCount) {
            this.segmentScaleFactors = this.segmentScaleFactors.slice(0, segmentCount);
        }
    }
    
    /**
     * 更新特效状态
     */
    private updateEffectStates(segmentCount: number, deltaTime: number): void {
        // 根据长度激活特效
        if (segmentCount >= this.PULSE_THRESHOLD) {
            if (!this.effects.has('pulse')) {
                this.effects.set('pulse', {
                    intensity: 0.2,
                    duration: Infinity,
                    color: '#ffffff'
                });
            }
        }
        
        if (segmentCount >= this.TRAIL_THRESHOLD) {
            if (!this.effects.has('trail')) {
                this.effects.set('trail', {
                    intensity: 0.3,
                    duration: Infinity,
                    color: '#aaccff'
                });
            }
        }
        
        if (segmentCount >= this.ENERGY_FIELD_THRESHOLD) {
            if (!this.effects.has('energyField')) {
                this.effects.set('energyField', {
                    intensity: 0.4,
                    duration: Infinity,
                    color: '#ffaa00'
                });
            }
        }
        
        if (segmentCount >= this.SPACE_DISTORTION_THRESHOLD) {
            if (!this.effects.has('spaceDistortion')) {
                this.effects.set('spaceDistortion', {
                    intensity: 0.5,
                    duration: Infinity,
                    color: '#ff00ff'
                });
            }
        }
        
        // 更新有限时长的特效
        for (const [effectType, params] of this.effects.entries()) {
            if (params.duration !== Infinity) {
                params.duration -= deltaTime;
                if (params.duration <= 0) {
                    this.effects.delete(effectType);
                }
            }
        }
    }
    
    /**
     * 更新粒子系统
     */
    private updateParticles(deltaTime: number): void {
        // 更新现有粒子
        for (let i = this.particles.length - 1; i >= 0; i--) {
            const particle = this.particles[i];
            
            // 更新生命周期
            particle.life -= deltaTime;
            
            // 移除死亡粒子
            if (particle.life <= 0) {
                this.particles.splice(i, 1);
                continue;
            }
            
            // 更新位置
            particle.position = particle.position.add(
                particle.velocity.multiply(deltaTime / 16)
            );
            
            // 随着生命周期减小粒子大小
            particle.size = particle.size * (particle.life / particle.maxLife);
        }
    }
    
    /**
     * 添加食物吞噬效果
     */
    public addConsumptionEffect(position: Vector2D, type: ConsumptionEffectType): void {
        switch (type) {
            case 'normal':
                this.createPulseEffect(position, '#ffffff', 500);
                break;
            case 'poison':
                this.createPulseEffect(position, '#aa00aa', 800);
                break;
            case 'magnet':
                this.createPulseEffect(position, '#ffcc00', 1000);
                break;
        }
    }
    
    /**
     * 创建脉冲效果
     */
    private createPulseEffect(position: Vector2D, color: string, duration: number): void {
        // 添加临时脉冲特效
        const pulseEffect: EffectParams = {
            intensity: 0.8,
            duration: duration,
            color: color
        };
        
        // 创建粒子爆发
        const particleCount = 20;
        for (let i = 0; i < particleCount; i++) {
            const angle = (Math.PI * 2 * i) / particleCount;
            const speed = 2 + Math.random() * 2;
            
            this.particles.push({
                position: new Vector2D(position.x, position.y),
                velocity: new Vector2D(
                    Math.cos(angle) * speed,
                    Math.sin(angle) * speed
                ),
                color: color,
                size: 3 + Math.random() * 2,
                life: 500 + Math.random() * 300,
                maxLife: 800
            });
        }
    }
    
    /**
     * 获取段缩放因子
     */
    public getSegmentScale(index: number): number {
        if (index >= 0 && index < this.segmentScaleFactors.length) {
            return this.segmentScaleFactors[index];
        }
        return this.BASE_SCALE;
    }
    
    /**
     * 获取活跃特效
     */
    public getActiveEffects(): Map<EffectType, EffectParams> {
        return new Map(this.effects);
    }
    
    /**
     * 获取粒子
     */
    public getParticles(): Array<{
        position: Vector2D;
        velocity: Vector2D;
        color: string;
        size: number;
        life: number;
        maxLife: number;
    }> {
        return [...this.particles];
    }
} 